package mediadisplay.timeline.contenteditarea 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import mediadisplay.timeline.TimelineContent;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class ContentEditArea extends Sprite
	{
		public var indexField:TextArea;
		public var textContent:TextArea;
		public var timeFrom:TextArea;
		public var timeTo:TextArea;
		
		private var allTextAreas:Array = new Array;
		private var _timelineContent:TimelineContent;
		public var editingIndex:int;
		
		public function ContentEditArea(timelineContent:TimelineContent) 
		{
			_timelineContent = timelineContent;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			indexField = new TextArea("index", true, 20);
			addChild(indexField);
			
			timeFrom = new TextArea("from", true, 40);
			addChild(timeFrom);
			timeFrom.x = 80;
			
			timeTo = new TextArea("to", true, 40);
			addChild(timeTo);
			timeTo.x = 180;
			
			textContent = new TextArea("Text content");
			addChild(textContent);
			textContent.y = 23;
			textContent.addEventListener(TextArea.TEXT_CHANGED, textChangedHandler);
			
			allTextAreas.push(textContent);
			
			addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
		}
		
		public function clear():void {
			trace("clear");
			indexField.setText("");
			timeFrom.setText("");
			timeTo.setText("");
			textContent.setText("");
			deactivateAll();
		}
		private function cleanup(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			textContent.removeEventListener(TextArea.TEXT_CHANGED, textChangedHandler);
			removeChild(textContent);
			removeChild(timeFrom);
			removeChild(timeTo);
		}
		
		private function textChangedHandler(e:Event):void 
		{
			_timelineContent.changeTextContent(editingIndex, textContent.getText());
			
		}
		
		public function deactivateAll():void {
			for (var i:int = 0; i < allTextAreas.length; i++) 
			{
				var item:TextArea = allTextAreas[i];
				item.deactivate();
			}
		}
		public function activateAll():void {
			for (var i:int = 0; i < allTextAreas.length; i++) 
			{
				var item:TextArea = allTextAreas[i];
				item.activate();
			}
		}
		public function edit(index:int):void {
			editingIndex = index;
			indexField.setText(String(index));
		}
		public function changeTimes(from:String, to:String):void {
			timeFrom.setText(from);
			timeTo.setText(to);
		}
	}

}